/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var otherServiceClass = require('../class/other-service.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var otherService = new otherServiceClass();

        var checkResult = otherService.checkParams(
            [
                'seller',
                'title',
                'serviceTime',
                'createUid'
            ],
            params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            otherService.init(params);
        }

        models.other_service.create(otherService.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },
    update: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var otherService = new otherServiceClass();
        var checkResult = otherService.checkParams(['rowId'], params);
        var checkScore = otherService.checkParams(['score'], params);

        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {

            if(!checkScore){

                var now = new Date();
                var jsonDate = now.toJSON();
                var then = new Date(jsonDate)
                params.scoreTime=then;
            }

            otherService.init(params);
        }


        models.other_service.update(params.rowId,otherService.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var otherService = new otherServiceClass();

        var checkResult = otherService.checkParams(['rowId'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            otherService.init(params);
        }

        models.other_service.findOne(otherService.getParams()).populate('seller').exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },

    getListBySeller: function (params, cb) {

        //console.log(params);

        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }
        var otherService = new otherServiceClass();

        var checkResult = otherService.checkParams(['seller'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            otherService.init(params);
        }
        models.other_service.find(otherService.getParams()).exec(function (err, data) {

            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    //获取其他服务列表
    getOtherServiceListbyServiceTime:function(params,cb){
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        models.other_service.find({sort: 'serviceTime desc'}).populate('seller').exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    //获取推荐列表卖家排序
    getOtherServiceListBySeller:function(params,cb){
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        //  skip: 20, limit: 10, sort: 'name DESC'
        models.other_service.find({sort: 'seller  asc'}).populate('seller').exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },

    //删除重点推荐
    deleteOtherService: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var otherService = new otherServiceClass();

        var checkResult = otherService.checkParams(['rowId'], params);

        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            otherService.init(params);
        }


        models.other_service.destroy(params.rowId,otherService.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    //更新重点推荐
    updateOtherService: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var otherService = new otherServiceClass();
        var checkResult = otherService.checkParams(
            [   'rowId',
                'seller',
                'title',
                'serviceTime',
                'createUid'
            ], params);

        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            otherService.init(params);
        }


        models.other_service.update(params.rowId,otherService.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    }

};



